﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ribbon.Web
{
    public enum ButtonStyle
    {
        TextOnly,
        ImageOnly,
        TextBesideImage,
        TextUnderImage
    }

    public class RibbonButton : RibbonItem , IButtonControl
    {
        #region [ fields ]
        private LinkButton _lbText;
        private Image _img;
        #endregion

        #region [ Constractors ]
        public RibbonButton()
        {
            _lbText = new LinkButton();
            _img = new Image();
            _lbText.ID = "lbl";
            _img.ID = "img";
        }
        #endregion

        #region [ overrides ]
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class,"RibbonSmallItem_Out");
            writer.AddAttribute("onmouseout", "this.className='RibbonSmallItem_Out'");
            writer.AddAttribute("onmouseover", "this.className='RibbonSmallItem_Over'");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.Width.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, this.Height.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, this.Align.ToString());
            writer.RenderBeginTag("div");

        }

        protected override void CreateChildControls()
        {   

            _img.Style.Add(HtmlTextWriterStyle.VerticalAlign,"middle");
            _img.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
            //---------------------------------------------------------
            
            if (this.ButtonStyle == ButtonStyle.TextBesideImage)
                _img.Style.Add(HtmlTextWriterStyle.Display, "inline");
            else
                _img.Style.Add(HtmlTextWriterStyle.Display, "block");

            if (this.ButtonStyle != ButtonStyle.TextOnly)
                _lbText.Controls.Add(_img);

            if (this.ButtonStyle != ButtonStyle.ImageOnly)
                _lbText.Controls.Add(new LiteralControl(Text));
            //--------------------------------------------------------
            Controls.Add(_lbText);
        }
        #endregion

        #region [ properties ]
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [UrlProperty]
        [Bindable(true)]
        [DefaultValue("")]
        public string ImageUrl
        {
            set
            {
                this._img.ImageUrl = value;
                //this.ViewState["imgeUrl"] = value;
            }
            get
            {
                return this._img.ImageUrl;
                //return this.ViewState["imgeUrl"] as string;
            }
        }

        [Bindable(true)]
        [DefaultValue(32)]
        public Unit ImageWidth
        {
            set
            {
                this._img.Width = value;
            }
            get
            {
                return this._img.Width;
            }
        }

        [Bindable(true)]
        [DefaultValue(32)]
        public Unit ImageHeight
        {
            set
            {
                this._img.Height = value;
            }
            get
            {
                return this._img.Height;
            }
        }

        [Bindable(true)]
        [DefaultValue(ButtonStyle.TextBesideImage)]
        public ButtonStyle ButtonStyle
        {
            set
            {
                this.ViewState["ButtonStyle"] = value;
            }
            get
            {
                var obj = this.ViewState["ButtonStyle"];
                return obj == null ? ButtonStyle.TextBesideImage : (ButtonStyle)obj;
            }
        }

        [Bindable(true)]
        [DefaultValue(HorizontalAlign.Center)]
        public HorizontalAlign Align
        {
            set
            {
                this.ViewState["Align"] = value;
            }
            get
            {
                var obj = this.ViewState["Align"];
                if (obj == null) return HorizontalAlign.Center;
                return (HorizontalAlign)obj;
            }
        }
        #endregion

        #region [ IButton Control ]

        /// <summary>
        /// Gets or sets a value indicating whether clicking the button causes page validation to occur.
        /// </summary>
        /// <returns>
        /// true if clicking the button causes page validation to occur; otherwise, false.
        /// </returns>
        public bool CausesValidation
        {
            get { return _lbText.CausesValidation; }
            set
            {
                _lbText.CausesValidation = value;
            }
        }

        /// <summary>
        /// Gets or sets an optional argument that is propagated to the <see cref="E:System.Web.UI.WebControls.IButtonControl.Command"/> event.
        /// </summary>
        /// <returns>
        /// The argument that is propagated to the <see cref="E:System.Web.UI.WebControls.IButtonControl.Command"/> event.
        /// </returns>
        public string CommandArgument
        {
            get { return _lbText.CommandArgument; }
            set
            {
                _lbText.CommandArgument = value;
            }
        }

        /// <summary>
        /// Gets or sets the command name that is propagated to the <see cref="E:System.Web.UI.WebControls.IButtonControl.Command"/> event.
        /// </summary>
        /// <returns>
        /// The name of the command that is propagated to the <see cref="E:System.Web.UI.WebControls.IButtonControl.Command"/> event.
        /// </returns>
        public string CommandName
        {
            get { return _lbText.CommandName; }
            set
            {
                _lbText.CommandName = value;
            }
        }

        /// <summary>
        /// Gets or sets the URL of the Web page to post to from the current page when the button control is clicked.
        /// </summary>
        /// <returns>
        /// The URL of the Web page to post to from the current page when the button control is clicked.
        /// </returns>
        public string PostBackUrl
        {
            get { return  _lbText.PostBackUrl ; }
            set
            {
                _lbText.PostBackUrl = value;
            }
        }

        /// <summary>
        /// Gets or sets the text caption displayed for the button.
        /// </summary>
        /// <returns>
        /// The text caption displayed for the button.
        /// </returns>
        public string Text
        {
            get { return _lbText.Text ; }
            set
            {
                _lbText.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the name for the group of controls for which the button control causes validation when it posts back to the server.
        /// </summary>
        /// <returns>
        /// The name for the group of controls for which the button control causes validation when it posts back to the server.
        /// </returns>
        public string ValidationGroup
        {
            get { return _lbText.ValidationGroup; }
            set
            {
                _lbText.ValidationGroup = value;
            }
        }

        public event EventHandler Click
        {
            add
            {
                _lbText.Click += value;
            }
            remove
            {
                _lbText.Click -= value;
            }
        }
        public event CommandEventHandler Command
        {
            add
            {
                _lbText.Command += value;
            }
            remove
            {
                _lbText.Command -= value;
            }
        }
        #endregion
    }
}